package com.ccf.business.etl.flow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ccf.business.etl.client.DatabaseClient;
import com.ccf.business.etl.model.dto.NodeDTO;
import com.ccf.business.etl.service.IRuleDictService;
import com.ccf.business.etl.service.IRuleGroupService;
import com.ccf.business.etl.service.IRuleSynonymService;
import com.common.utils.tool.StringUtil;
import com.component.api.model.flow.NodeInfo;
import com.component.api.model.param.ComponentDefine;
import com.component.api.model.param.ParamPair;
import com.component.api.utils.ComponentUtils;
import com.framework.minio.config.MinioConfig;
import com.framework.minio.service.MinioService;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * 参数处理者
 * @author xianggj
 * @Date 2021/11/30 14:35
 **/
@Setter
public abstract class ParamHandler  {

    protected DatabaseClient databaseClient;

    protected IRuleGroupService iRuleGroupService;

    protected IRuleSynonymService iRuleSynonymService;

    protected IRuleDictService iRuleDictService;

    protected MinioConfig minioConfig;

    protected MinioService minioService;

    protected NodeDTO nodeDTO;

    protected Map<String, String> getParams(){
        JSONObject jsonObject = JSON.parseObject(nodeDTO.getParams());
        Map<String, String> params = new HashMap<>();
        if (jsonObject == null){
            return params;
        }
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()){
            params.put(entry.getKey(), StringUtil.toString(entry.getValue()));
        }
        return params;
    }


    /**
     * 参数处理
     */
    protected abstract Map<String, String>  finishingParam();

    /**
     * 分析数据
     * @return
     */
    public String analysis() {
        List<ParamPair> res = new ArrayList<>();
        ComponentDefine componentDefine = ComponentUtils.getComponentDefineByRule(nodeDTO.getCode());
        Map<String, String> map = finishingParam();
        List<ParamPair> outParams = componentDefine.getOutParams();
        for (ParamPair p : outParams) {
            ParamPair clone = p.clone();
            clone.setValue(map.get(clone.getName()));
            res.add(clone);
        }
        return JSON.toJSONString(res);
    }

    /**
     * 过去值
     * @return
     */
    public NodeInfo get(){
        NodeInfo nodeInfo = NodeInfo.builder().name(nodeDTO.getName())
                .id(nodeDTO.getId()).rule(nodeDTO.getCode()).build();
        nodeInfo.setParam(analysis());
        return nodeInfo;
    }

}
